//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <bsp.h>
#include <init.h>
#include <regs.h>

#include "pic.h"

EXTERN_C DECL_CODEINIT void CDECL InitPIC()
{
    BspMaskAllIrqs(FALSE);
}

EXTERN_C irqs_t CDECL BspGetIrqs()
{
    uint32_t    uStatus;
    uint32_t    uCause;

    read_cp0_register($12, uStatus);
    read_cp0_register($13, uCause);

    return ((uCause & uStatus) >> 8) & 0xff;
}

INLINE void EndOfInterrupt(irq_t irq)
{
}

EXTERN_C void CDECL BspMaskIrqs(irqs_t irqs, bool_t bEnable)
{
    unsigned int uRes;

    read_cp0_register($12, uRes);   // CP0_STATUS
    if (bEnable) {
        uRes |= (irqs << 8);
    }
    else {
        uRes &= ~(irqs << 8);
    }
    write_cp0_register($12, uRes);  // CP0_STATUS
}

EXTERN_C void CDECL BspMaskAllIrqs(bool_t bEnable)
{
    unsigned int uRes;

    read_cp0_register($12, uRes);   // CP0_STATUS
    if (bEnable) {
        uRes = uRes | SR_IM;
    }
    else {
        uRes &= ~SR_IM;
    }
    write_cp0_register($12, uRes);  // CP0_STATUS
}

EXTERN_C void CDECL BspAcknowledgeIrq(irq_t irq)
{
    EndOfInterrupt(irq);
}

